const dayjs = require('dayjs')

const phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const pwdReg = /^[\w]{6,12}$/

const JWT_SECRET = 'yangkunzuishuai';

const formatNowDate = () => {
    return dayjs().format('YYYY-MM-DD HH:mm:ss')
}

//数组去重
const uniqueFunc = (targetArr) => {
    let arr = [];
    targetArr.forEach(item => {
        if (!arr.includes(item)) {
            arr.push(item)
        }
    })
    return arr;
}

const scoreFilter = (scoreArr, targetScore) => {
    return scoreArr.filter(item => {
        return item.score === targetScore
    })
}

//自定义长度为length的字符串
const generateRandomString = (length) => {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return result;
}

const randomString = generateRandomString(5);
console.log(randomString);

module.exports = {
    phoneReg,
    pwdReg,
    JWT_SECRET,
    formatNowDate,
    uniqueFunc,
    scoreFilter,
    generateRandomString
}
